package com.itheima.sfbx.insurance.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.sfbx.framework.commons.exception.ProjectException;
import com.itheima.sfbx.framework.commons.utils.BeanConv;
import com.itheima.sfbx.framework.commons.utils.EmptyUtil;
import com.itheima.sfbx.framework.commons.utils.ExceptionsUtil;
import com.itheima.sfbx.insurance.dto.SafeguardVO;
import com.itheima.sfbx.insurance.enums.SafeguardEnum;
import com.itheima.sfbx.insurance.mapper.SafeguardMapper;
import com.itheima.sfbx.insurance.pojo.Safeguard;
import com.itheima.sfbx.insurance.service.ISafeguardService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
public class SafeguardServiceImpl extends ServiceImpl<SafeguardMapper, Safeguard> implements ISafeguardService {
    @Override
    public Page<SafeguardVO> findPage(SafeguardVO safeguardVO, int pageNum, int pageSize) {
        Page<Safeguard> safeguardPage = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<Safeguard> queryWrapper = new LambdaQueryWrapper<>();
        //根据保障项编号
        queryWrapper.eq(!EmptyUtil.isNullOrEmpty(safeguardVO.getSafeguardKey()),
                Safeguard::getSafeguardKey, safeguardVO.getSafeguardKey());
        //根据保障项名称
        queryWrapper.like(!EmptyUtil.isNullOrEmpty(safeguardVO.getSafeguardKeyName()),
                Safeguard::getSafeguardKeyName, safeguardVO.getSafeguardKeyName());
        //状态
        queryWrapper.eq(!EmptyUtil.isNullOrEmpty(safeguardVO.getDataState()),
                Safeguard::getDataState, safeguardVO.getDataState());
        //按照创建时间降序排
        queryWrapper.orderByDesc(Safeguard::getCreateTime);

        Page<Safeguard> resultPage = page(safeguardPage, queryWrapper);

        //转换为VO
        Page<SafeguardVO> safeguardVOPage = BeanConv.toPage(resultPage, SafeguardVO.class);
        return safeguardVOPage;
    }

    @Override
    public SafeguardVO findById(String safeguardId) {
        return null;
    }

    @Override
    public SafeguardVO save(SafeguardVO safeguardVO) {
        try {
            //将safeguardVO转换为safeguard
            Safeguard safeguard = BeanConv.toBean(safeguardVO, Safeguard.class);
            //保存数据
            boolean flag = save(safeguard);
            if (!flag) {
                throw new RuntimeException("保存保障项失败！");
            }
            //将safeguard转换为safeguardVO并返回
            return BeanConv.toBean(safeguard, SafeguardVO.class);

        } catch (Exception e) {
            log.error("保存保障项失败：{}", ExceptionsUtil.getStackTraceAsString(e));
            throw new ProjectException(SafeguardEnum.SAVE_FAIL);
        }
    }

    @Override
    public Boolean update(SafeguardVO safeguardVO) {
        try {
            //将safeguardVO转换为safeguard
            Safeguard safeguard = BeanConv.toBean(safeguardVO, Safeguard.class);

            //更新并返回更新结果
            boolean flag = updateById(safeguard);
            if (!flag) {
                throw new RuntimeException("更新保障项失败！");
            }
            return flag;
        } catch (Exception e) {
            log.error("更新保障项失败：{}", ExceptionsUtil.getStackTraceAsString(e));
            throw new ProjectException(SafeguardEnum.UPDATE_FAIL);
        }
    }

    @Override
    public Boolean delete(String[] checkedIds) {
        try {
            List<Long> idsList = Arrays.asList(checkedIds).stream().map(Long::new).collect(Collectors.toList());
            boolean flag = removeByIds(idsList);
            if (!flag) {
                throw new RuntimeException("批量删除保障项失败！");
            }
            return flag;
        } catch (RuntimeException e) {
            log.error("删除保障项异常；{}", ExceptionsUtil.getStackTraceAsString(e));
            throw new ProjectException(SafeguardEnum.DEL_FAIL);
        }
    }

    @NotNull
    private static LambdaQueryWrapper<Safeguard> queryWrapper(SafeguardVO safeguardVO) {
        LambdaQueryWrapper<Safeguard> queryWrapper = new LambdaQueryWrapper<>();
        //根据保障项编号
        queryWrapper.eq(!EmptyUtil.isNullOrEmpty(safeguardVO.getSafeguardKey()),
                Safeguard::getSafeguardKey, safeguardVO.getSafeguardKey());
        //根据保障项名称
        queryWrapper.like(!EmptyUtil.isNullOrEmpty(safeguardVO.getSafeguardKeyName()),
                Safeguard::getSafeguardKeyName, safeguardVO.getSafeguardKeyName());
        //状态
        queryWrapper.eq(!EmptyUtil.isNullOrEmpty(safeguardVO.getDataState()),
                Safeguard::getDataState, safeguardVO.getDataState());
        //按照创建时间降序排
        queryWrapper.orderByDesc(Safeguard::getCreateTime);
        return queryWrapper;
    }

    @Override
    public List<SafeguardVO> findList(SafeguardVO safeguardVO) {
        try {
            LambdaQueryWrapper<Safeguard> queryWrapper = queryWrapper(safeguardVO);
            List<Safeguard> list = list(queryWrapper);
            if (!EmptyUtil.isNullOrEmpty(list)) {
                return BeanConv.toBeanList(list, SafeguardVO.class);
            }
        } catch (Exception e) {
            log.error("查询保障项列表失败；{}", ExceptionsUtil.getStackTraceAsString(e));
            throw new ProjectException(SafeguardEnum.LIST_FAIL);
        }
        return List.of();
    }

    @Override
    public List<SafeguardVO> findShowPageItemByKey(List<String> safeguardKeyList) {
        return null;
    }

    @Override
    public SafeguardVO findBySafeguardKey(String safeguardKey) {
        return null;
    }

}